package com.itheima.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.goods.pojo.Category;
import com.itheima.goods.service.CategoryService;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @GetMapping
    public List<Category> findAll(){
        return categoryService.list();
    }

    @PutMapping
    public Result<Category> add(@RequestBody Category category){
        categoryService.save(category);
        return new Result(true,20000,"新增成功");
    }

    @GetMapping("search/{page}/{size}")
    public Result<Category> findPage(@PathVariable("page") Integer page,
                                     @PathVariable("size") Integer size){
        Page<Category> page1 = new Page<>(page, size);
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.ge("id",2);
        Category category = categoryService.getById(wrapper);
        return new Result<>(true,20000,"查询成功");
    }

    @GetMapping("/{id}")
    public Result<Category> findById(@PathVariable("id") Integer id){
        Category byId = categoryService.getById(id);
        return new Result<>(true,20000,"查询成功");
    }

    @PutMapping("/{id}")
    public Result<Category> update(@RequestBody Category category,@PathVariable("id")Integer id){
        LambdaQueryWrapper<Category> wrapper = Wrappers.<Category>lambdaQuery();
        wrapper.eq(Category::getId,category.getId());
        categoryService.update(category,wrapper);
        return new Result<>(true,20000,"更新成功");
    }

    @DeleteMapping("/{id}")
    public Result<Category> delete(@PathVariable("id")Integer id){
        categoryService.removeById(id);
        return new Result<>(true,20000,"删除成功");
    }
}
